package com.litepaltest.test;

import java.util.HashSet;
import java.util.Set;

import junit.framework.Assert;

import org.litepal.tablemanager.Connector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.litepaltest.model.Classroom;
import com.litepaltest.model.IdCard;
import com.litepaltest.model.Student;

public class SaveTestCase extends AndroidTestCase {

	@Override
	protected void setUp() throws Exception {

	}

	public void testSaveSingleModel() {
		Classroom c = new Classroom();
		c.setName("Music room");
		c.save();
		Classroom c2 = new Classroom();
		c2.setName("Art room");
		c2.save();
		Classroom c3 = new Classroom();
		c3.setName("Math room");
		c3.save();
		Assert.assertTrue(isDataExists("Classroom", c.get_id()));
		Assert.assertTrue(isDataExists("Classroom", c2.get_id()));
		Assert.assertTrue(isDataExists("Classroom", c3.get_id()));
	}

	public void testOne2One() {
		IdCard idcard1 = new IdCard();
		idcard1.setNumber("320300");
		idcard1.setAddress("Tom Street");
		Student tom = new Student();
		tom.setName("Tom");
		tom.setAge(14);
		tom.setIdcard(idcard1);
		tom.save();
		Assert.assertTrue(isDataExists("Idcard", idcard1.getId()));
		Assert.assertTrue(isDataExists("Student", tom.getId()));
	}

	public void testMany2Many() {
		// Student danny = new Student();
		// danny.setName("Danny");
		// danny.setAge(14);
		// Student mick = new Student();
		// mick.setName("Mick");
		// mick.setAge(13);
		// Teacher cam = new Teacher();
		// cam.setTeacherName("Cam");
		// cam.setAge(33);
		// cam.setSex(true);
		// cam.setTeachYears(5);
		// Teacher jack = new Teacher();
		// jack.setTeacherName("Jack");
		// jack.setAge(36);
		// jack.setSex(false);
		// jack.setTeachYears(11);
		//
		// danny.getTeachers().add(jack);
		// danny.getTeachers().add(cam);
		//
		// mick.getTeachers().add(jack);
		// mick.getTeachers().add(cam);
		//
		// cam.getStudents().add(danny);
		// cam.getStudents().add(mick);
		//
		// jack.getStudents().add(danny);
		// jack.getStudents().add(mick);
		//
		// danny.save();
	}

	public void testCascadingSaving() {
		IdCard idcard1 = new IdCard();
		idcard1.setNumber("320300");
		idcard1.setAddress("stree1");
		IdCard idcard2 = new IdCard();
		idcard2.setNumber("320301");
		idcard2.setAddress("stree2");
		IdCard idcard3 = new IdCard();
		idcard3.setNumber("320302");
		idcard3.setAddress("stree3");
		IdCard idcard4 = new IdCard();
		idcard4.setNumber("320303");
		idcard4.setAddress("stree4");
		Classroom englishRoom = new Classroom();
		englishRoom.setName("English room");
		Student tom = new Student();
		tom.setName("Tom");
		tom.setAge(14);
		tom.setIdcard(idcard1);
		tom.setClassroom(englishRoom);
		Student jim = new Student();
		jim.setName("Jim");
		jim.setAge(14);
		jim.setIdcard(idcard2);
		jim.setClassroom(englishRoom);
		tom.save();
		jim.save();
		Assert.assertTrue(isDataExists("Classroom", englishRoom.get_id()));
		Assert.assertTrue(isDataExists("Student", tom.getId()));
		Assert.assertTrue(isDataExists("Student", jim.getId()));
		Classroom computerRoom = new Classroom();
		computerRoom.setName("Computer room");
		Student lily = new Student();
		lily.setName("Lily");
		lily.setAge(13);
		lily.setIdcard(idcard3);
		Student lucy = new Student();
		lucy.setName("Lucy");
		lucy.setAge(13);
		lucy.setIdcard(idcard4);
		Set<Student> studentCollection = new HashSet<Student>();
		studentCollection.add(lily);
		studentCollection.add(lucy);
		computerRoom.setStudentCollection(studentCollection);
		computerRoom.save();
		Assert.assertTrue(isDataExists("Classroom", computerRoom.get_id()));
		Assert.assertTrue(isDataExists("Student", lily.getId()));
		Assert.assertTrue(isDataExists("Student", lucy.getId()));
		Assert.assertTrue(isDataExists("Idcard", idcard1.getId()));
		Assert.assertTrue(isDataExists("Idcard", idcard2.getId()));
		Assert.assertTrue(isDataExists("Idcard", idcard3.getId()));
		Assert.assertTrue(isDataExists("Idcard", idcard4.getId()));
	}

	private boolean isDataExists(String table, long id) {
		SQLiteDatabase db = Connector.getDatabase();
		Cursor cursor = null;
		try {
			cursor = db.query(table, null, "id = ?", new String[] { String.valueOf(id) }, null,
					null, null);
			return cursor.getCount() == 1 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
		}
		return false;
	}
}
